﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Evolution
{
    static class IniReader
    {
        public static string ReadSection(StreamReader InputFile,ref byte[] Arguments, string[] Strings)
        {
            if (Arguments.Length != Strings.Length) return "[Error]";
            string Str;
            while (true)
            {
                Str = InputFile.ReadLine();
                bool Flag = true;
                for (int i = 0; i < Str.Length; i++)
                    Flag = Flag && ((Str[i] == ' ') || (Str[i] == '\t'));
                if (Flag) continue;
                if (Str[0] == '[')
                    return Str;
                int Begin = 0; int End;
                while ((Begin<Str.Length)&&((Str[Begin]==' ')||(Str[Begin]=='\t'))) 
                    Begin++;
                int Length=0;
                while ((Begin+Length<Str.Length)&&(Str[Begin+Length]!=' ')&&(Str[Begin+Length]!='\t')&&(Str[Begin+Length]!='=')) 
                    Length++;
                End = Begin + Length;
                while ((End < Str.Length) && (Str[End] != '='))
                    End++;
                End++;
                for (int i = 0; i < Strings.Length; i++)
                {
                    if (Length == Strings[i].Length)
                    {
                        Flag = true;
                        for (int j=0;j<Length;j++)
                            Flag=Flag && (Str[Begin+j]==Strings[i][j]);
                        if (Flag)
                        {
                            string Temp="";
                            int k = End;
                            while (k < Str.Length)
                            {
                                Temp += Str[k];
                                k++;
                            }
                            Arguments[i] = Convert.ToByte(Temp);
                        }
                    }                 
                }

            }
        }
    }
}
